<!--
Copyright (c) 2019-2026, Hossein Moein
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.
* Neither the name of Hossein Moein and/or the DataFrame nor the
  names of its contributors may be used to endorse or promote products
  derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Hossein Moein BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE html>
<html>
<body>

    <table align="center" border="1">

    <tr bgcolor="lightblue">
      <th>Signature</th> <th>Description</th>
    </tr>
    <tr bgcolor="lightgrey">
      <td bgcolor="maroon"> <font color="white">
        <PRE><B>
enum class bucket_type : unsigned char  {
    by_distance = 1, // Bucketize by distance between two index values (i.g. X<sub>2</sub> - X<sub>1</sub> = N)
    by_count = 2,    // Bucketize by counting of index values (e.g. every N index items)
}; </B></PRE> </font>
      </td>
      <td>
        This determines the bucketization logic
      </td>
    </tr>

    </table>

    <BR>

  <table align="center" border="1">

    <tr bgcolor="lightblue">
        <th>Signature</th> <th>Description</th> <th>Parameters</th>
    </tr>

    <tr bgcolor="lightgrey">
      <td bgcolor="maroon"> <font color="white">
        <PRE><B>
template&lt;typename V, typename I_V, typename ... Ts&gt;
DataFrame
bucketize(bucket_type bt,
          const V &amp;value,
          I_V &amp;&amp;idx_visitor,
          Ts&amp;&amp; ... args) const;
        </B></PRE></font>
      </td>
      <td WIDTH="33.3%">
        It bucketizes the data and index into intervals, based on index values and bucket_type.<BR>
        You must specify how the index column is bucketized, by providing a visitor.<BR>
        You must specify how each column is bucketized, by providing 3-member tuples (triples). Each triple must have the following members:<BR>
        <OL>
           <LI>Current DataFrame column name</LI>
           <LI>Column name for the new bucketized DataFrame</LI>
           <LI>A visitor to aggregate/bucketize current column to new column</LI>
        </OL>

        The result of each bucket will be stored in a new DataFrame and returned. Some data at the end of source columns may not be included in the result columns, because based on bucket_type they may not fit into the bucket. The index of each bucket will be determined by idx_visitor.<BR>
      </td>
      <td WIDTH="33.3%">
        <B>V</B>: Type of value to be uased for bucketizing based on bucket_type<BR>
        <B>I_V</B>: Type of visitor to be used to bucketize the index column<BR>
        <B>Ts</B>: Types of triples to specify each column's bucketization<BR>
        <B>bt</B>: bucket_type to specify bucketization logic<BR>
        <B>value</B>: The value to be uased to bucketize based on bucket_type. For example, if bucket_type is by_distance, then value is the distance between two index values. If bucket_type is by_count, then value is an integer count.<BR>
        <B>idx_visitor</B>: A visitor to specify the index bucketization<BR>
        <B>args</B>: Variable argument list of triples as specified above<BR>
      </td>
    </tr>

    <tr bgcolor="lightgrey">
      <td bgcolor="maroon"> <font color="white">
        <PRE><B>
template&lt;typename V, typename I_V, typename ... Ts&gt;
std::future&lt;DataFrame&gt;
bucketize_async(bucket_type bt,
                const V &amp;value,
                I_V &amp;&amp;idx_visitor,
                Ts&amp;&amp; ... args) const;
        </B></PRE></font>
      </td>
      <td WIDTH="33.3%">
        Same as bucketize() above, but executed asynchronously
      </td>
      <td WIDTH="33.3%">
      </td>
    </tr>

  </table>

<pre style='color:#000000;background:#ffffff;'><span style='color:#800000; font-weight:bold; '>static</span> <span style='color:#800000; font-weight:bold; '>void</span> test_bucketize<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>  <span style='color:#800080; '>{</span>

    <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>cout</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> <span style='color:#800000; '>"</span><span style='color:#0f69ff; '>\n</span><span style='color:#0000e6; '>Testing bucketize( ) ...</span><span style='color:#800000; '>"</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>endl</span><span style='color:#800080; '>;</span>

    MyDataFrame df<span style='color:#800080; '>;</span>

    <span style='color:#800000; font-weight:bold; '>try</span>  <span style='color:#800080; '>{</span>
        df<span style='color:#808030; '>.</span>read<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD.csv</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> io_format<span style='color:#800080; '>::</span>csv2<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#800000; font-weight:bold; '>auto</span>        fut <span style='color:#808030; '>=</span>
            df<span style='color:#808030; '>.</span>bucketize_async<span style='color:#808030; '>(</span>bucket_type<span style='color:#800080; '>::</span>by_distance<span style='color:#808030; '>,</span>
                               <span style='color:#008c00; '>100</span><span style='color:#808030; '>,</span>
                               LastVisitor<span style='color:#800080; '>&lt;</span>MyDataFrame<span style='color:#800080; '>::</span>IndexType<span style='color:#808030; '>,</span> MyDataFrame<span style='color:#800080; '>::</span>IndexType<span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span> 
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Date</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Date</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> LastVisitor<span style='color:#800080; '>&lt;</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>string</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>High</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> MaxVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Low</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> MinVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Open</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> FirstVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> LastVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Mean</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> MeanVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Std</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> StdVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Volume</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Volume</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> SumVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>long</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        MyDataFrame result <span style='color:#808030; '>=</span> fut<span style='color:#808030; '>.</span>get<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        result<span style='color:#808030; '>.</span>write<span style='color:#800080; '>&lt;</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>ostream</span><span style='color:#808030; '>,</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>string</span><span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>double</span><span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>long</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>cout</span><span style='color:#808030; '>,</span> io_format<span style='color:#800080; '>::</span>csv2<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// FORD index is just an increasing number starting from 0.</span>
        <span style='color:#696969; '>// So, by_count should give the same result as by_distance</span>
        <span style='color:#696969; '>//</span>
        <span style='color:#800000; font-weight:bold; '>auto</span>        fut2 <span style='color:#808030; '>=</span>
            df<span style='color:#808030; '>.</span>bucketize_async<span style='color:#808030; '>(</span>bucket_type<span style='color:#800080; '>::</span>by_count<span style='color:#808030; '>,</span>
                               <span style='color:#008c00; '>100</span><span style='color:#808030; '>,</span>
                               LastVisitor<span style='color:#800080; '>&lt;</span>MyDataFrame<span style='color:#800080; '>::</span>IndexType<span style='color:#808030; '>,</span> MyDataFrame<span style='color:#800080; '>::</span>IndexType<span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span> 
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Date</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Date</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> LastVisitor<span style='color:#800080; '>&lt;</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>string</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>High</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> MaxVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Low</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> MinVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Open</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> FirstVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> LastVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Mean</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> MeanVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Std</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> StdVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>,</span>
                               <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>make_tuple<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>FORD_Volume</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Volume</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> SumVisitor<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>long</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        MyDataFrame result2 <span style='color:#808030; '>=</span> fut2<span style='color:#808030; '>.</span>get<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        assert<span style='color:#808030; '>(</span>result<span style='color:#808030; '>.</span>is_equal<span style='color:#808030; '>(</span>result2<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
    <span style='color:#800000; font-weight:bold; '>catch</span> <span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>const</span> DataFrameError <span style='color:#808030; '>&amp;</span>ex<span style='color:#808030; '>)</span>  <span style='color:#800080; '>{</span>
        <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>cout</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> ex<span style='color:#808030; '>.</span>what<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>endl</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
<span style='color:#800080; '>}</span>
</pre>
<!--Created using ToHtml.com on 2021-04-01 14:03:55 UTC -->

  <img src="https://github.com/hosseinmoein/DataFrame/blob/master/docs/LionLookingUp.jpg?raw=true" alt="C++ DataFrame"
       width="200" height="150" style="float:right"/>

</body>
</html>

<!--
Local Variables:
mode:HTML
tab-width:4
c-basic-offset:4
End:
-->
